package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/*
 * 分类管理
 * */
@RestController
@RequestMapping("/admin/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /*
     * 分类的分页查询
     * */
    @GetMapping("/page")
    public Result<PageResult> pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageResult pageResult = categoryService.pageQuery(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 新增分类
     */
    @PostMapping
    public Result<String> save(@RequestBody CategoryDTO categoryDTO){
        categoryService.save(categoryDTO);
        return Result.success();
    }
    /*
     * 根据id删除分类
     * */
    @DeleteMapping
    public Result<String> deleteById(Long id) {

        categoryService.deleteById(id);
        return Result.success();
    }

    /*
     * 修改分类
     * */
    @PutMapping
    public Result<String> UpdateCategory(@RequestBody CategoryDTO categoryDTO) {
        categoryService.UpdateCategory(categoryDTO);
        return Result.success();
    }

    /*
     * 启用和禁用分类状态
     * */
    @PostMapping("/status/{status}")
    public Result<String> UpdateCategoryStatus(@PathVariable Integer status, Long id) {
        categoryService.UpdateCategoryStatus(status, id);
        return Result.success();
    }

    /**
     * 根据类型查询分类
     */
    @GetMapping("/list")
    public Result<List<Category>> list(Integer type){
        List<Category> list = categoryService.list(type);
        return Result.success(list);
    }
}
